package com.hoioy.findjob.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.ServiceLoader;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}

public class ZkTest1 {
    private static String aaa = "";

    private int b = 0;
    public static void main(String[] args) throws InterruptedException {
//        nomral();
//        curatorTest();
//        Float a = new Float(1.0);
//
//       Lock aa = new ReentrantLock();
//       Condition dd = aa.newCondition();

        ZkTest1 z1 = new ZkTest1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    if(z1.b==1){
                        System.out.println("z1.b:"+z1.b);
                    }
                }
            }
        }).start();
        Thread.sleep(1000);
        z1.b = 1;
        Thread.sleep(1000);
        z1.b = 2;
//        for(int i=0;i<250;i++){
//            new ZkTest1().tst();
//        }
    }

    public static void nomral() {
        try {
            final CountDownLatch countDownLatch=new CountDownLatch(1);
            ZooKeeper zooKeeper=
                    new ZooKeeper("8.214.10.202:2181,8.214.10.202:2182,8.214.10.202:2183",
                            4000, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            if(Event.KeeperState.SyncConnected==event.getState()){
                                //如果收到了服务端的响应事件，连接成功
                                countDownLatch.countDown();
                            }
                        }
                    });
            countDownLatch.await();
            //CONNECTED
            System.out.println(zooKeeper.getState());
            zooKeeper.create("/zhaozhao","0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }

    }

    public static void curatorTest(){
        CuratorFramework curatorFramework= CuratorFrameworkFactory.
                builder().connectString("8.214.10.202:2181,8.214.10.202:2182,8.214.10.202:2183").
                sessionTimeoutMs(4000).retryPolicy(new
                ExponentialBackoffRetry(1000,3)).
                namespace("").build();
        curatorFramework.start();
        Stat stat=new Stat();
        //查询节点数据
        byte[] bytes = new byte[0];
        try {
            bytes = curatorFramework.getData().storingStatIn(stat).forPath("/zhaozhao");

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new String(bytes));
        curatorFramework.close();
    }
}
